package com.lewei.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

/**
 * 时间按年月日加减控件
 * @author 22901
 *
 */
public class JDateChooser extends JDialog implements ActionListener {

	
	public JDateChooser(Dialog parent, String title, Point location,
			int showYears) {
		super(parent, title, true);
		this.setTitle(title);
		this.location = location;
		getContentPane().setLayout(null);
		
		
		// init
		this.initDatas();
	}
	public JDateChooser(JFrame parent, String title, Point p) {
		// TODO Auto-generated constructor stub
		super(parent, title, true);
		this.location = location;
		// init
		this.initDatas();
	}
	/**
	 * 
	 * @param width
	 *            长度
	 * @param height
	 *            宽度
	 * @return
	 */
	private Dimension getStartDimension(int width, int height) {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		dim.width = dim.width / 2 - width / 2;
		dim.height = dim.height / 2 - height / 2;
		return dim;
	}
	private void initDatas() {
		// TODO Auto-generated method stub
		this.chooser = this;
		
	}
	
	/**
	 * 显示Dialog的方法
	 */
	private void showDialog() {
		// 设置布局
		getContentPane().setLayout(null);
		
		final JLabel label3 = new JLabel("\u5E74\uFF1A");
		label3.setText(date);
		label3.setBounds(100, 10, 80, 20);
		getContentPane().add(label3);
		
		JLabel label = new JLabel("\u5E74\uFF1A");
		label.setBounds(46, 38, 54, 15);
		getContentPane().add(label);
		
		JLabel label_1 = new JLabel("\u6708\uFF1A");
		label_1.setBounds(46, 76, 54, 15);
		getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("\u65E5\uFF1A");
		label_2.setBounds(46, 122, 54, 15);
		getContentPane().add(label_2);
		
		JButton btn_del_year = new JButton("-");
		btn_del_year.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_del_year.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_year.setText((Integer.parseInt(txt_year.getText())-1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(1, -1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btn_del_year.setBounds(79, 34, 39, 23);
		getContentPane().add(btn_del_year);
		
		JButton btn_del_moon = new JButton("-");
		btn_del_moon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_del_moon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_moon.setText((Integer.parseInt(txt_moon.getText())-1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(2, -1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btn_del_moon.setBounds(79, 72, 39, 23);
		getContentPane().add(btn_del_moon);
		
		JButton btn_del_day = new JButton("-");
		btn_del_day.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_del_day.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_day.setText((Integer.parseInt(txt_day.getText())-1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(5, -1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btn_del_day.setBounds(79, 118, 39, 23);
		getContentPane().add(btn_del_day);
		
		txt_year = new JTextField();
		txt_year.setEditable(false);
		txt_year.setText("0");
		txt_year.setBounds(123, 35, 50, 21);
		getContentPane().add(txt_year);
		txt_year.setColumns(10);
		
		txt_moon = new JTextField();
		txt_moon.setEditable(false);
		txt_moon.setText("0");
		txt_moon.setColumns(10);
		txt_moon.setBounds(123, 73, 50, 21);
		getContentPane().add(txt_moon);
		
		txt_day = new JTextField();
		txt_day.setEditable(false);
		txt_day.setForeground(Color.BLACK);
		txt_day.setText("0");
		txt_day.setColumns(10);
		txt_day.setBounds(123, 119, 50, 21);
		getContentPane().add(txt_day);
		
		JButton btn_add_year = new JButton("+");
		btn_add_year.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_add_year.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_year.setText((Integer.parseInt(txt_year.getText())+1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(1, 1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btn_add_year.setBounds(183, 34, 45, 23);
		getContentPane().add(btn_add_year);
		
		JButton btn_add_moon = new JButton("+");
		btn_add_moon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_add_moon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_moon.setText((Integer.parseInt(txt_moon.getText())+1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(2, 1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btn_add_moon.setBounds(183, 72, 45, 23);
		getContentPane().add(btn_add_moon);
		
		JButton btn_add_day = new JButton("+");
		btn_add_day.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_add_day.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				txt_day.setText((Integer.parseInt(txt_day.getText())+1)+"");
				try {
					gc.setTime(sdf.parse(date));
					gc.add(5, 1);
					date=sdf.format(gc.getTime());
					label3.setText(date);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btn_add_day.setBounds(183, 118, 45, 23);
		getContentPane().add(btn_add_day);
		
		JButton button = new JButton("\u786E\u5B9A");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				
				//					gc.setTime(sdf.parse(date));
//					gc.add(1, Integer.parseInt(txt_year.getText().trim()));
//					gc.add(2, Integer.parseInt(txt_moon.getText().trim()));
//					gc.add(5, Integer.parseInt(txt_day.getText().trim()));
//					date=sdf.format(gc.getTime());
				dater=date;
				chooser.dispose();
				
			}
		});
		button.setBounds(60, 170, 80, 30);
		getContentPane().add(button);
		
		JButton btn_re = new JButton("重置");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btn_re.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
					date=sdf.format(new Date());
					label3.setText(date);
					txt_year.setText("0");
					txt_moon.setText("0");
					txt_day.setText("0");
			}
		});
		btn_re.setBounds(150, 170, 80, 30);
		getContentPane().add(btn_re);
		// 设置大小
				setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
				// 设置显示的位置
				if (this.location == null) {
					Dimension dim = getStartDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
					setLocation(dim.width, dim.height);
				} else {
					setLocation(this.location);
				}
			setVisible(true);
	}
	/**
	 * 在构造对象后调用此方法获取选择的日历值
	 * 
	 * @return 选择日历对象Calendar
	 */
	public String showCalendarDialog(String date) {
		this.date=date;
		this.dater=date;
		this.showDialog();
		return this.dater;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}
	// 默认宽度与高度
		private static final int DEFAULT_WIDTH = 285;
		private static final int DEFAULT_HEIGHT = 280;
		private Point location=null;
		private String title = "选择日期";
		private JDateChooser chooser=null;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		private String date=sdf.format(new Date());
		private String dater=sdf.format(new Date());
		private JTextField txt_year;
		private JTextField txt_moon;
		private JTextField txt_day;
		GregorianCalendar gc=new GregorianCalendar();
}
